#!/usr/bin/env Rscript
################################
## Generalization Error
## estimation for fraud paper
## K-fold, Forensic features 
################################


# Flag to send job to cluster or 
# run in a regular computer (still in parallel,
# using MPI backend)
PegasusRun  <- commandArgs()

if(PegasusRun){
  system("bsub < GenErrorkFoldForen.job")
}else{
  #!/usr/bin/env Rscript
  
  ## Load libraries on all cores
  library(plyr)
  library(foreach)
  library(doMPI)

  
  ## Seed RNG
  set.seed(831213)
  
  ## Start cluster & worker loop
  cl  <- startMPIcluster()
  registerDoMPI(cl)
  
  ## Load libraries on master
  library(BayesTree)
  library(glmnet)
  
  ## Load auxiliary functions
  source("./GenErrorAuxFunc.R")
  
  ## Get processed data
  load("./FullData.RData")
  
  
  ## Estimate cross-validated generalization errors
  ## Set nr. of folds
  n.folds<- 15
  
  ### Set parameters to best paramsweep values
  bestParams <- read.csv("./BSParamSweep.csv"
                         ,nrows=1
  )[,1:6]
  bartParams  <- expand.grid(k=bestParams$k
                             ,pow=bestParams$pow
                             ,base=bestParams$base
                             ,sigdf=bestParams$sigdf
                             ,sigquant=bestParams$sigquant
                             ,ntree=bestParams$ntree
                             ,sampind=1:n.folds
                             ,burnin=50e3
                             ,niter=5e3
                             ,keepevery=10)
  bartParams  <- as.matrix(bartParams)
  
  ## Estimate generalization errors
  ## for forensics-only model
  ## using 15-fold CV
  foren.errors.boot  <- err.generator(n.folds
                                      ,bartParams
                                      ,all.data$fDataForen
                                      ,all.data$countries.foren
                                      ,parallel=TRUE
                                      ,kfold=TRUE)
  
  save(foren.errors.boot
       ,file="./GenErrorsKFoldForen.RData")
  
  closeCluster(cl)
  mpi.finalize()
}